﻿Imports System.Runtime.CompilerServices

Namespace Global.System.IO.IsolatedStorage

    Public Module Extension

        <Extension()> _
        Public Sub DeleteDirectory(ByVal [Me] As IsolatedStorageFile, ByVal dirName As String, ByVal recursive As Boolean)
            If recursive Then
                Dim stk As New Stack(Of String)
                stk.Push(dirName)
                While stk.Count > 0
                    Dim currentPath As String = stk.Pop()
                    Dim currentFiles = [Me].GetFileNames(IO.Path.Combine(currentPath, "*.*"))
                    Dim currentDirs = [Me].GetDirectoryNames(IO.Path.Combine(currentPath, "*.*"))
                    For Each fi In currentFiles
                        [Me].DeleteFile(IO.Path.Combine(currentPath, fi))
                    Next
                    If currentDirs.Length = 0 Then
                        [Me].DeleteDirectory(currentPath)
                    End If
                    For Each d In currentDirs
                        stk.Push(IO.Path.Combine(currentPath, d))
                    Next
                End While
            Else
                [Me].DeleteDirectory(dirName)
            End If
        End Sub

    End Module

End Namespace